import axios from 'axios';
import {toTreeData} from '../../utils/treeData'

const MENU_COLLAPSED = "MENU_COLLAPSED";
const MENU_EXTENDED = "MENU_EXTENDED";
const MENU_LOAD_DATA = "MENU_LOAD_DATA";
const BUTTON_LOAD_DATA = "BUTTON_LOAD_DATA";
const INITIALIZEMENU="INITIALIZEMENU";

const initState = {
    collapsed: false,
    menus: [],
    defaultSelectedKeys: [],
    defaultOpenKeys: [],
    button:[],
    extraMenu:[
        {id:"001001001",parentId:"001001",url:"/company/AddCompany",name:"新增(修改)企业"},
        {id:"003001001",parentId:"003001",url:"/powerStation/chargeOrder",name:"充电订单"},
        {id:"003002002",parentId:"003001",url:"/powerStation/baseInfo",name:"电站信息"},
        {id:"003002002",parentId:"003000",url:"/powerStation/baseInfo",name:"电站信息"},
        {id:"003002003",parentId:"003001",url:"/powerStation/priceInfo",name:"电价信息"},
        {id:"003002001",parentId:"003002",url:"/powerStation/equChargeOrder",name:"充电订单"},
        {id:"004001001",parentId:"004001",url:"/order/chargeProcess",name:"充电过程"},
        {id:"007003001",parentId:"007003",url:"/staff/addCar",name:"添加车辆"},
        {id:"007001001",parentId:"007001",url:'/staff/addStaff',name:"添加员工"},
        {id:"006001001",parentId:"006001",url:"/finance/staffTotal",name:"充值统计"},
        {id:"006003001",parentId:"006003",url:"/finance/BillDetails4Month",name:'账单详情'},
        {id:"006003001001",parentId:"006003001",url:"/finance/OrderDetails4Months",name:'订单明细'},
        {id:"008002001",parentId:"008002",url:'/company/addAccount',name:"添加子账号"},
        {id:"006002001",parentId:"006002",url:'/finance/BillDetails',name:'账单详情'},
        {id:"006002001001",parentId:"006002001",url:"/finance/OrderDetails",name:'订单明细'},
    ]
}

// reducer
export function menu(state = initState, action) {

    switch (action.type) {
        case MENU_COLLAPSED:
            return {...state, collapsed: true};
        case MENU_EXTENDED:
            return {...state, collapsed: false};
        case MENU_LOAD_DATA:
            return {...state, ...action.payload};
        case BUTTON_LOAD_DATA:
            return {...state, ...action.payload};
        case INITIALIZEMENU:
            return {...state, ...action.payload};
        default:
            return state;
    }
}

// action creator
export function initializeMenu() {
    return {type: INITIALIZEMENU, payload: {menus: []}};
}

function loadData(data, originData, defaultSelectedKeys, defaultOpenKeys) {
    originData=JSON.parse(originData);
    originData=originData.concat(initState.extraMenu);
    return {type: MENU_LOAD_DATA, payload: {menus: data, menusList: originData, defaultSelectedKeys, defaultOpenKeys}};
}


function loadButton(data) {
    return {type: BUTTON_LOAD_DATA, payload: {button: data}};
}

function collapsed() {
    return {type: MENU_COLLAPSED};
}

function extended() {
    return {type: MENU_EXTENDED};
}

// 折叠或展开菜单栏
export function actionCollapsed(flag) {
    return flag ? collapsed() : extended();
}

// 加载菜单操作
export function loadMenus({userId, defaultUrl}) {
    return dispatch => {
        // 后端需按照id顺序排序
        axios.post('/api/admin/corp/menu/qry', {
            userId,
        }).then(res => {
            if (res && res.code==200) {
                var test=JSON.stringify(res.data.menu);
                let data = [];
                let defaultSelectedKeys = [];
                let defaultOpenKeys = [];
                res.data.menu.map(function (v, index) {
                    v.children = [];
                    if (v.parentId === '000') {
                        data.push(v);
                    } else if (index > 0) { // 从第二个菜单开始判断
                        if (defaultUrl === v.url) { // 设置默认选中的菜单
                            defaultSelectedKeys = [v.url];
                            defaultOpenKeys = [v.parentId];
                        }
                        let last = index - 1;
                        if (v.parentId === res.data.menu[last].id) {
                            res.data.menu[last].children.push(v);
                            v.parent = res.data.menu[last];
                        } else if (v.parentId === res.data.menu[last].parentId
                            && res.data.menu[last].parent) {
                            v.parent = res.data.menu[last].parent;
                            v.parent.children.push(v);
                        }
                    }
                    return v;
                });
                dispatch(loadButton(res.data.button));
                dispatch(loadData(data, test, defaultSelectedKeys, defaultOpenKeys));
            }
        });
    }
}